﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class Photo_Review : PageBase
{
    static decimal count = -1, start_record = 0, end_record = 10, pages = 0, pagesize = 10, curpage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtHidOrg.Value = OrgHidValue;
            this.txtOrg.Text = OrgShowValue;
            InitForm();

            txtStartDate.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
            txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            btnQuery_Click(null, null);
        }
    }

    private void InitForm()
    {
        DictionaryItemsManager dim = new DictionaryItemsManager();
        //绑定用户
        DataTable dt = new Jumpton.SFA.Server.BusinessRule.MobileUserManager().Load("", this.MyStateManager.DomainID,MyStateManager.OrgID);
        ddlMobileUser.DataTextField = "CODENAME";
        ddlMobileUser.DataValueField = "ID";
        ddlMobileUser.DataSource = dt;
        ddlMobileUser.DataBind();
        ddlMobileUser.Items.Insert(0, new ListItem("--所有--", ""));

        //客户类型
        ddl_customertype.DataTextField = "Name";
        ddl_customertype.DataValueField = "DictionaryItemId";
        ddl_customertype.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        ddl_customertype.DataBind();
        ddl_customertype.Items.Insert(0, new ListItem(Resources.language.ddl_Select, ""));

        //照片类型
        ddlPhotoType.DataTextField = "Name";
        ddlPhotoType.DataValueField = "CODE";
        ddlPhotoType.DataSource = dim.GetItemList(DictionaryEnum.PhotoType, this.MyStateManager.DomainID);
        ddlPhotoType.DataBind();
        ddlPhotoType.Items.Insert(0, new ListItem(Resources.language.ddl_Select, ""));
    }

    public void BindFrom(decimal s,decimal e )
    {

        string where = "";
        if (ViewState["where"] != null)
            where = ViewState["where"].ToString();
        //dlPhotos.DataSource = PhotoManager.GetPhotoList(MyStateManager.DomainID,where);
        dlPhotos.DataSource = PhotoManager.GetByPage(MyStateManager.DomainID, s, e,where);
        dlPhotos.DataBind();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        string where = "";
        if (ddlMobileUser.SelectedValue != "")
        {
            where = " AND P.USER_ID='" + ddlMobileUser.SelectedValue + "'";
        }
        if (ddlPhotoType.SelectedValue != "")
        {
            where += " AND P.PHOTO_TYPE_ID='" + ddlPhotoType.SelectedValue + "'";
        }
        if (ddl_customertype.SelectedValue != "")
        {
            where += " AND C.CUST_CLASS_CODE='" + ddl_customertype.SelectedValue + "'";
        }
        if (txtCustomer.Text != "")
        {
            where += " AND (C.NAME LIKE '%" + txtCustomer.Text + "%' OR C.CODE LIKE  '%" + txtCustomer.Text + "%')";
        }
        if (txtHidOrg.Value != "")
        {
            where += " AND C.ORG_ID IN(" + txtHidOrg.Value + ")";
        }
        if (txtStartDate.Text != "" && txtEndDate.Text != "")
        {
            where += " AND V.START_TIME BETWEEN " + BusinessLogic.GetStringDateTimeDecimal(txtStartDate.Text) + "000000 AND " + BusinessLogic.GetStringDateTimeDecimal(txtEndDate.Text) + "240000";
        }
        
        count = PhotoManager.GetCount(MyStateManager.DomainID,where);
        ViewState["where"] = where;
        if (count > 0)
        {
            pages = Decimal.Floor(count / pagesize);//总页数
            pages = (count / pagesize) > pages ? (pages + 1) : pages;
            start_record = 1;
            end_record = pagesize;
            curpage = 1;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }
        else
        {
            start_record = 0;
            end_record = 0;
            pages = 0;
            curpage = 0;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", 0, 0, 0);
           
            // BindFrom(0, 0);
        }

        BindFrom(start_record, end_record);
    }

    protected void Unnamed1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
       
    }

    public string FormatURL(object id)
    {
        return ("../IMAGE.ASPX?" + id);
    }


    protected void btn_first_Click(object sender, EventArgs e)
    {

        if (curpage != 1 && count > 0)
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }


    }
    protected void btn_per_Click(object sender, EventArgs e)
    {
        if (curpage > 1)
        {
            end_record = start_record - 1;
            start_record = start_record - pagesize;

            curpage--;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }
    }
    protected void btn_next_Click(object sender, EventArgs e)
    {
        if (curpage < pages)
        {
            start_record = end_record + 1;
            end_record = end_record + pagesize;
            curpage++;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);
        }
    }
    protected void btn_end_Click(object sender, EventArgs e)
    {
        if (curpage < pages)
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;

            for (int i = 1; i < pages; i++)
            {
                start_record = end_record + 1;
                end_record = end_record + pagesize;
                curpage++;
            }

            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);

        }
    }
    protected void btnGoto_Click(object sender, EventArgs e)
    {
        int page = 0;
        try
        {
            page = int.Parse(txtPage.Text.Trim());
        }
        catch { page = 0; }
        if (page > 0 & page <= pages)
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;

            for (int i = 1; i < page; i++)
            {
                start_record = end_record + 1;
                end_record = end_record + pagesize;
                curpage++;
            }

            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);

        }

    }

    public string getPhotoType(string v)
    {
        ListItem li = ddlPhotoType.Items.FindByValue(v);
        if (li != null)
            return li.Text;
        else
            return "";

    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string startime=DateTime.Parse(txtStartDate.Text).ToString("yyyyMMddHHmmss");
        string endtime= DateTime.Parse(txtEndDate.Text).AddDays(1).ToString("yyyyMMddHHmmss");
        if (BeyondDataDiff(startime, endtime))
        {
            Response.Write("<script>alert('所选时间超出8天，请重新选择!')</script>");
            return;
        }
        string query = string.Empty;
        query = query + "?BeginTime=" +startime ;
        query += "&EndTime=" + endtime;
        query += "&CutomerName=" + txtCustomer.Text;
        query += "&user=" + ddlMobileUser.SelectedValue;
        query += "&flag=visitDetail";

        Response.Redirect("ExportPhoto.aspx" + query);
    }

    /// <summary>
    /// 判断时间差是否大于8天
    /// </summary>
    /// <param name="DateTime1">开始时间</param>
    /// <param name="DateTime2">结束时间</param>
    /// <returns></returns>
    public bool BeyondDataDiff(string time1,string time2)
    {
        bool flag = false;
        DateTime DateTime1 = Convert.ToDateTime(BusinessLogic.GetDateTime(time1, "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss"));
        DateTime DateTime2 = Convert.ToDateTime(BusinessLogic.GetDateTime(time2, "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss"));
        bool flage = true;
        StringBuilder sb = new StringBuilder();
        TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
        TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
        TimeSpan ts = ts1.Subtract(ts2).Duration();
        if (ts.Days > 8)
        {
            flag = true;
        }
        return flag;
    }
}
